# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("stargazer", "tidyverse", "estimatr"))

library(stargazer)
library(tidyverse)
library(estimatr)

study_1 <- read_csv("study_1.csv")
study_2 <- read_csv("study_2.csv")
study_3 <- read_csv("study_3.csv")

study_1 <- study_1 %>% mutate(Z_ad = as.numeric(Z_ad == "spanish_ad"))

# Table 5 -----------------------------------------------------------------

fit_0_b <- lm(bush_general ~ Z_ad + Z_survey, data = filter(study_1, bilingual == 1))
fit_1_b <- lm(vela_general ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_1_m <- lm(vela_general ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_vela)))
fit_2_b <- lm(coffman_general ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))
fit_2_m <- lm(coffman_general ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_0_b, fit_1_b, fit_1_m, fit_2_b, fit_2_m,
          p = starprep(fit_0_b, fit_1_b, fit_1_m, fit_2_b, fit_2_m, stat = "p"),
          se = starprep(fit_0_b, fit_1_b, fit_1_m, fit_2_b, fit_2_m),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Constant (Control Mean)"),
          dep.var.labels = c("Bush: General Election", "Vela: General Election", "Coffman: General Election"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Bilingual", "Monolingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Ad on General Election Support",
          label = "general_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))


# Table 6 -----------------------------------------------------------------

fit_3_b <- lm(like_bush ~ Z_ad + Z_survey , data = filter(study_1, bilingual == 1))
fit_4_b <- lm(like_vela ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_4_m <- lm(like_vela ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_vela)))
fit_5_b <- lm(like_coffman ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))
fit_5_m <- lm(like_coffman ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_3_b, fit_4_b, fit_4_m, fit_5_b, fit_5_m,
          p = starprep(fit_3_b, fit_4_b, fit_4_m, fit_5_b, fit_5_m, stat = "p"),
          se = starprep(fit_3_b, fit_4_b, fit_4_m, fit_5_b, fit_5_m),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Constant (Control Mean)"),
          dep.var.labels = c("Like Bush", "Like Vela", "Like Coffman"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Bilingual", "Monolingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Ad on Liking Candidate (1-7)",
          label = "like_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))



# Table 7 -----------------------------------------------------------------

fit_6_b <- lm(bush_cares ~ Z_ad + Z_survey, data = filter(study_1, bilingual == 1))
fit_7_b <- lm(vela_cares ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_7_m <- lm(vela_cares ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_vela)))
fit_8_b <- lm(coffman_cares ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))
fit_8_m <- lm(coffman_cares ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_6_b, fit_7_b, fit_7_m, fit_8_b, fit_8_m,
          p = starprep(fit_6_b, fit_7_b, fit_7_m, fit_8_b, fit_8_m, stat = "p"),
          se = starprep(fit_6_b, fit_7_b, fit_7_m, fit_8_b, fit_8_m),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Constant (Control Mean)"),
          dep.var.labels = c("Bush Cares", "Vela Cares", "Coffman Cares"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Bilingual", "Monolingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Ad on Perceptions of Candidate Caring",
          label = "care_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))


# Table 8 -----------------------------------------------------------------

fit_9_b <- lm(conf_in_bush ~ Z_ad + Z_survey, data = filter(study_1, bilingual == 1))
fit_10_b <- lm(conf_in_vela ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_10_m <- lm(conf_in_vela ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_vela)))
fit_11_b <- lm(conf_in_coffman ~ Z_ad + Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))
fit_11_m <- lm(conf_in_coffman ~ Z_ad, data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_coffman)))


stargazer(fit_9_b, fit_10_b, fit_10_m, fit_11_b, fit_11_m,
          p = starprep(fit_9_b, fit_10_b, fit_10_m, fit_11_b, fit_11_m, stat = "p"),
          se = starprep(fit_9_b, fit_10_b, fit_10_m, fit_11_b, fit_11_m),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Constant (Control Mean)"),
          dep.var.labels = c("Confidence in Bush", "Confidence in Vela", "Confidence in Coffman"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Bilingual", "Monolingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Ad on \n Confidence in Candidate to Make Right Decisions about Illegal Immigration",
          label = "confidence_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))


# Table 9 -----------------------------------------------------------------

fit_12_b <- lm(linked_fate ~ Z_survey, data = filter(study_1, bilingual == 1))
fit_13_b <- lm(linked_fate ~ Z_survey, data = filter(study_2, bilingual == 1))

stargazer(fit_12_b,fit_13_b,
          p = starprep(fit_12_b,fit_13_b, stat = "p"),
          se = starprep(fit_12_b,fit_13_b),
          covariate.labels = c("Spanish-language Survey", "Constant (Control Mean)"),
          dep.var.labels = c("Linked Fate"),
          add.lines = list(c("Sample", "Bilingual (Bush Experiment)", "Bilingual (Vela and Coffman Experiments)")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Survey on Linked Fate",
          label = "linked_fate",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))
